<template>
    <div>
        <Form :label-width="100" ref="formInline" :model="formInline">
            <Row :gutter="16" class="query-form2" >
                <i-col span="8">
                    <FormItem label="年度：">
                        <DatePicker v-model="formInline.year" type="year" placeholder="请选择年度" style="width: 100%"></DatePicker>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <FormItem label="姓名：">
                        <Input v-model="formInline.userName" placeholder="请填写姓名"/>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <div style="float: right;">
                        <Button class="ivu-btn-query"  icon="md-search"  @click="query">查询</Button>
                        <Button class="ivu-btn-reset" style="margin-left: 10px" @click="cancel">重置</Button>
                    </div>
                </i-col>
            </Row>
        </Form>
        <Row class="query-form">
            <i-col span="24">
                <Button size="large" class="ivu-btn-add" icon="md-cloud-download" @click="exportFile">导出</Button>
            </i-col>
        </Row>
        <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="columns" no-data-text="没有查询到相关数据！"
               :data="infos" width="100%" :height="tableHeight" highlight-row stripe size="small"></Table>
    </div>
</template>

<script>

    import {Api} from '../../../api'
    import {sessionStorage} from '@/common/cacheUtils.js';

    export default {
        name: "securityDirectorCount",
        data(){
            return {
                loading: false,
                formInline: {},
                columns: [
                    {
                        type: 'index',
                        title: '序号',
                        width: 80,
                        align: 'center',
                        fixed: 'left',
                    },
                    {
                        title: '安全总监',
                        align: 'center',
                        key: 'userName',
                        minWidth: 200,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.userName || '--')
                            ]);
                        }
                    },
                    {
                        title: '集团安全审计',
                        align: 'center',
                        key: 'auditMissionCount',
                        minWidth: 200,
                        sortable: true
                    },
                    {
                        title: '二级集团安全审计',
                        align: 'center',
                        key: 'auditTwoMissionCount',
                        minWidth: 200,
                        sortable: true
                    },
                    {
                        title: '安全环保检查',
                        align: 'center',
                        key: 'ckeckCount',
                        minWidth: 200,
                        sortable: true
                    },
                    {
                        title: '人员行为观察',
                        align: 'center',
                        key: 'recordCount',
                        minWidth: 200,
                        sortable: true
                    },
                ],
                infos: [],
                tableHeight: ''
            }
        },
        mounted(){
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 105;
            this.query();
        },
        methods: {
            query(){
                this.loading = true;
                if(this.formInline.year != ''){
                    this.formInline.year = this.formatDate(this.formInline.year, 'yyyy')
                }
                this.$http.get(Api.securityDirectorInfo + '/count', {
                    params: this.formInline
                }).then(function(res){
                    this.infos = res.data.data;
                    this.loading = false;
                }, function(res){
                    this.$Message.error('请求失败');
                });
            },
            cancel(){
                this.formInline = {};
                this.query();
            },
            exportFile(){
                if(this.formInline.year != ''){
                    this.formInline.year = this.formatDate(this.formInline.year, 'yyyy')
                }
                this.$http.get(Api.securityDirectorInfo + '/count/excel',{
                    params: this.formInline,
                    responseType: 'arraybuffer'
                }).then((res) => {
                    const aLink = document.createElement("a");
                    let blob = new Blob([res.data], {
                        type: "application/vnd.ms-excel"
                    })
                    aLink.href = URL.createObjectURL(blob);
                    aLink.download = "安全总监工作统计.xlsx" //标题
                    aLink.click();
                    document.body.appendChild(aLink);
                })
            },
            formatDate(date, fmt) {
                if (typeof date == 'string') {
                    return date;
                }
                if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
                if (!date || date == null) return null;
                let o = {
                    'M+': date.getMonth() + 1, // 月份
                    'd+': date.getDate(), // 日
                    'h+': date.getHours(), // 小时
                    'm+': date.getMinutes(), // 分
                    's+': date.getSeconds(), // 秒
                    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
                    'S': date.getMilliseconds() // 毫秒
                }
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
                for (let k in o) {
                    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                }
                return fmt
            },
        }
    }
</script>

<style lang='css' scoped>
    @import '../../../index.css';

</style>
